package Game;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.*;

@SuppressWarnings("serial")
public class Game extends JPanel {

    Ast b = new Ast(this);
    Teclado t = new Teclado(this);
    int con = 0;
    Calendar tiempo = Calendar.getInstance();
    static int colorito = 0;
    static Image ico;
    private ArrayList<String> scoretxt;

    private Game() {
        scoretxt = new ArrayList();
        CargaGuardaTxT.fin(scoretxt);
        icono();
        KeyListener oe = new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
                t.TecLib(e);
            }

            @Override
            public void keyPressed(KeyEvent e) {
                t.TecPres(e);
            }
        };
        addKeyListener(oe);
        setFocusable(true);
    }

    private void mo() {
        b.mover();
    }

    public void resNav() {
        t.res();
    }

    public void gameOver() {
        if (con >= 2) {
            String temp = "" + b.getScore();
            if (temp.length() == 2) {
                temp = "000" + temp;
            } else if (temp.length() == 3) {
                temp = "00" + temp;
            } else if (temp.length() == 4) {
                temp = "0" + temp;
            }else{
                temp = "0000" + temp;
            }
            scoretxt.add("+---------------------------------+");
            scoretxt.add("|   Player                        |");
            scoretxt.add("|   Score:   "+temp+"                |");
            scoretxt.add("+---------------------------------+");
            CargaGuardaTxT.fin(scoretxt);
            JOptionPane.showMessageDialog(this,
                    "Game Over\nScore: " + b.getScore(), "Asteroides",
                    JOptionPane.YES_NO_OPTION);
            con = -1;
            colorito = 0;
            b.setScore();
        }
        con++;
        b.resetColisiones();
        t.choque();
        Ast.tiempoRes = Calendar.getInstance();
        Ast.empezar = false;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(new Color(colorito, colorito, colorito));
        g.fillRect(0, 0, 150, 600);
        g.setColor(Color.GRAY);
        g.fillRect(155, 0, 300, 600);
        g.setColor(Color.white);
        score(g2d);
        b.pintar(g2d);
        t.pintar(g2d);
    }

    public void score(Graphics2D g) {
        String rgc = "SCORE";
        String scor = Integer.toString(b.getScore());
        g.setFont(g.getFont().deriveFont(20f));
        g.drawString(rgc, 192, 50);
        g.drawString(scor, 220, 90);
        String vidas = "VIDAS";
        g.drawString(vidas, 195, 150);
        if (con == 0) {
            g.drawString("3", 220, 190);
        } else if (con == 1) {
            g.drawString("2", 220, 190);
        } else if (con == 2) {
            g.drawString("1", 220, 190);
        }
        g.drawString("A", 192, 325);
        g.drawString("D", 252, 325);
    }

    public static void main(String[] args) throws InterruptedException {
        JFrame f = new JFrame("Asteroides");
        Game g = new Game();
        f.add(g);
        f.setSize(300, 600);
        f.setResizable(false);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setIconImage(ico);
        boolean bo = true;
        while (bo) {
            g.mo();
            g.repaint();
            Thread.sleep(20);
        }
    }

    private void icono() {
        URL pathIcon = this.getClass().getClassLoader().getResource("images/nave.png");
        Toolkit kit = Toolkit.getDefaultToolkit();
        ico = kit.createImage(pathIcon);
    }

}
